import random

def read_file():
    '''
        功能：读取文件
        参数：无
        返回值：读取文件后生成的字典
    '''
    with open('game_one_user.txt','r') as f:
        date_list = f.readline().split()
        #print(date_list)
        date_dic = {date_list[0]:date_list[1:]}
        print(date_dic[date_list[0]][2])
        ave_time = int(date_dic[date_list[0]][1]) / int(date_dic[date_list[0]][0])
        print('{},你已经玩了{num}次，最少{num1}轮猜出答案，平均{num2:.2f}轮猜出答案，开始游戏！'.format(date_list[0], num=int(date_dic[date_list[0]][0]), num1=int(date_dic[date_list[0]][2]),
                                                                            num2=float(ave_time)))
        return date_dic

def play_game(dic):
    '''
        功能：运行游戏
        参数：read_file()函数返回的字典
        返回值：无
    '''
    for name in dic:
        name = name
        lis = dic[name]
    times = int(lis[0])
    lis1 = [int(lis[2])]
    n = int(lis[1])
    while True:
        a = random.randint(1,100)
        times += 1
        total =0
        while True:
            try:
                num = int(input('Guess a number(1-100):'))
                total += 1
                n+=1
                if num > a:
                    print('Too big, please try again.')
                elif num < a:
                    print('Too small, please try again.')
                else:
                    print('Bingo!')
                    lis1.append(total)
                    ave_time = n / times
                    least = min(lis1)
                    print('谢谢！%s,你一共玩了%d次游戏, 一共猜了%d轮,平均%.2f轮猜出, 最少%d轮猜出答案.' % (name,times, n, ave_time,least))
                    result = '%s %d %d %d' % (name, times, n, least)
                    break
            except ValueError:
                print('请输入数字！')
                continue
        i = input('Continue(Y/N):')
        if i.lower() != 'y':
            break
    with open('game_one_user.txt', 'w') as f:
        f.write(result)

def main():
    '''主函数'''
    date_dic = read_file()
    play_game(date_dic)



if __name__ == '__main__':
    main()
